﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjetSMA.Model
{
    class Predator : ItemMove
    {

        private bool m_has_hunting = false;
        private bool m_has_bred = false;
        private ItemMove m_target;

        public Predator(Element par_position_now, float par_growth, string par_name)
            : base(par_position_now, par_growth, par_name)
        {
            m_range++;
            m_range_perception--;
        }

        public override void Live()
        {
            base.Live();
       

            if (m_elements_visible.Count > 0)
            {
                this.DeterminedPerception();
                this.Detection();
                this.SortItemMoveViewed();

                // they wait the good age to breed
                if (m_item_friends.Count > 0 && m_cpt_child < m_cpt_child_max && m_growth == m_growth_max)
                {
                    // desire of breeding
                    m_want_breed = true;
                    m_decision.UpdateList("_breeding", 1, true);
                }

                if (m_item_foes.Count > 0)
                {
                    // increase his starvation
                    m_decision.UpdateList("_hunting", 2, true);
                }
                else
                {
                    // He needs hunting to avoid starvation                
                    m_decision.UpdateList("_hunting", 1, false);
                }

                m_type_decision = m_decision.ChoseDecision();

                switch (m_type_decision)
                {
                    case "_breeding":
                        CaseBreedind();
                        break;

                    case "_hunting":
                        CaseHunting();
                        break;

                    case "_nothing":
                        MoveRandom();
                        m_decision.ResetState();
                        base.Live();
                        break;
                }
            }
            else
            {
                MoveRandom();               
                base.Live();
            }

        }

        public void CaseHunting()
        {
            if (m_has_hunting)
            {
                // check if another predator killed it before
                if (m_target != null)
                {
                    m_target.Killed = true;
                    m_position_now.Occupants.Remove(m_target);
                }

                m_decision.ResetValue("_hunting", 0);
                m_has_hunting = false;

                m_starvation = 0;

                //base.Live();
            }
            else
                Hunt();

            m_decision.ResetState();
        }
        public void Hunt()
        {          
            if (InListNeighboors(m_item_foes[0].PositionNow) || m_position_now == m_item_foes[0].PositionNow)
                m_has_hunting = true;

            if (m_position_now != m_item_foes[0].PositionNow)
            {
                m_target = m_item_foes[0];
                Aetoile(m_item_foes[0].PositionNow);                
            }
        }
        public void CaseBreedind()
        {
            if (m_has_bred)
            {
                if (m_growth == m_growth_max && m_item_friends[0].WantBreed == true)
                {
                    m_cpt_child++;
                    m_children.Add(new Predator(m_position_now, 1, m_name + m_cpt_child));
                    m_decision.ResetValue("_breeding", 0);
                }

                m_has_bred = false;
                m_want_breed = false;
            }
            else
            {
                Breed();               
            }

            m_decision.ResetState();
        }
        public void Breed()
        {
            if (InListNeighboors(m_item_friends[0].PositionNow) || m_position_now == m_item_friends[0].PositionNow)
                m_has_bred = true;

            if (m_position_now != m_item_friends[0].PositionNow)
                Aetoile(m_item_friends[0].PositionNow);                
        }

        public void SortItemMoveViewed()
        {
            foreach (ItemMove loc_item in m_item_viewed)
            {
                if (loc_item != this)
                {
                    if (TestPrey(loc_item))
                        m_item_foes.Add(loc_item);
                    else
                        m_item_friends.Add(loc_item);
                }
            }
        }

        private bool TestPrey(Object o)
        {
            if (o is Prey)
                return true;
            return false;
        }   
    }
}
